package com.fortune.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MChannelDiscountExcution;
import com.fortune.dto.in.mChannelDiscount.MChannelDiscountAddDto;
import com.fortune.dto.in.mChannelDiscount.MChannelDiscountDeleteDto;
import com.fortune.dto.in.mChannelDiscount.MChannelDiscountEditDto;
import com.fortune.dto.in.mChannelDiscount.MChannelDiscountListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MChannelDiscountEnum;
import com.fortune.service.MChannelDiscountServer;
import com.fortune.vo.mChannelDiscount.MChannelDiscount;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "m_channel_discount", description = "特殊折扣相关", tags = {"特殊折扣相关"})
@RestController
@RequestMapping("mChannelDiscount")
public class ChannelDiscountController {
	
	@Autowired
	private MChannelDiscountServer mChannelDiscountServer;
	
	@ApiOperation(value = "折扣列表", nickname = "list", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
		@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
		@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string")
	})
    @GetMapping(value = "list",produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<MChannelDiscount>> list(HttpServletRequest request,@Validated @ApiIgnore MChannelDiscountListDto listDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		listDto.setPage((listDto.getPage()-1)*10);
		List<MChannelDiscount> list = mChannelDiscountServer.list(mUserInfo,listDto);
		int count = mChannelDiscountServer.count(mUserInfo, listDto);
		PagingData<MChannelDiscount> pagingData = new PagingData<>(list, count);
		return new MChannelDiscountExcution<PagingData<MChannelDiscount>>(pagingData, MChannelDiscountEnum.SUCCESS);
	}
	@ApiOperation(value = "新增折扣",nickname ="add",notes = "",tags = {"@岳希伟"})
	@PostMapping(value = "add",
	        produces = { MediaType.APPLICATION_JSON_VALUE },
	        consumes = { MediaType.APPLICATION_JSON_VALUE }) 
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody MChannelDiscountAddDto addDto){
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		int result = mChannelDiscountServer.add(mUserInfo,addDto);
		if (result==1) {
			return new MChannelDiscountExcution<Void>(null, MChannelDiscountEnum.SUCCESS);
		}else if (result==2) {
			return new MChannelDiscountExcution<Void>(null, MChannelDiscountEnum.CHECKFALSE);
		}
		return new MChannelDiscountExcution<Void>(null, MChannelDiscountEnum.FALSE);
	}
	@ApiOperation(value = "编辑折扣",nickname ="edit",notes = "",tags = {"@岳希伟"})
	@PutMapping(value = "edit",
	        produces = { MediaType.APPLICATION_JSON_VALUE },
	        consumes = { MediaType.APPLICATION_JSON_VALUE }) 
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody MChannelDiscountEditDto editDto){
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		int result = mChannelDiscountServer.edit(mUserInfo,editDto);
		if (result==1) {
			return new MChannelDiscountExcution<Void>(null, MChannelDiscountEnum.SUCCESS);
		}else if (result==0) {
			return new MChannelDiscountExcution<Void>(null, MChannelDiscountEnum.FALSE);
		}
		return new MChannelDiscountExcution<Void>(null, MChannelDiscountEnum.NOCHECK);
	}
	@ApiOperation(value = "删除折扣",nickname ="edit",notes = "",tags = {"@岳希伟"})
	@PutMapping(value = "delete",
	        produces = { MediaType.APPLICATION_JSON_VALUE },
	        consumes = { MediaType.APPLICATION_JSON_VALUE }) 
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @RequestBody MChannelDiscountDeleteDto deleteDto){
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		int result = mChannelDiscountServer.delete(mUserInfo,deleteDto);
		if (result==1) {
			return new MChannelDiscountExcution<Void>(null, MChannelDiscountEnum.SUCCESS);
		}else if (result==0) {
			return new MChannelDiscountExcution<Void>(null, MChannelDiscountEnum.FALSE);
		}else if (result==2) {
			return new MChannelDiscountExcution<Void>(null, MChannelDiscountEnum.DELETENULL);
		}
		return new MChannelDiscountExcution<Void>(null, MChannelDiscountEnum.DELETEEXIST);
	}	
}
